package com.gbiac.modules.monitor.service.impl;

import com.gbiac.modules.monitor.mapper.ViolationTypeStatisticsMapper;
import com.gbiac.modules.monitor.model.query.ViolationTypeStatisticsQuery;
import com.gbiac.modules.monitor.model.vo.*;
import com.gbiac.modules.monitor.service.ViolationTypeStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Description: 违规类型统计服务
 * @Author: fengchengguo
 * @Date:   2024-11-30
 * @Version: V1.0
 */
@Service
@Slf4j
public class ViolationTypeStatisticsServiceImpl implements ViolationTypeStatisticsService {

    @Resource
    private ViolationTypeStatisticsMapper violationTypeStatisticsMapper;
    /**
     * 线程池
     */
    private final Executor executors = new ThreadPoolExecutor(17, 34, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10000), new ThreadPoolExecutor.CallerRunsPolicy());


    @Override
    public ViolationTypeStatisticsVO query(ViolationTypeStatisticsQuery violationTypeStatisticsQuery) {
        ViolationTypeStatisticsVO violationTypeStatisticsVO = new ViolationTypeStatisticsVO();

        String queryDate = violationTypeStatisticsQuery.getQueryDate();
        if(queryDate.contains("-")){
            violationTypeStatisticsQuery.setFormat("%Y-%m");
        }else {
            violationTypeStatisticsQuery.setFormat("%Y");
        }
        CompletableFuture<Void> query_1 = CompletableFuture.runAsync(() -> {
            List<ViolationTypeStatisticsUserVO> userList = violationTypeStatisticsMapper.queryUserList(violationTypeStatisticsQuery);
            violationTypeStatisticsVO.setUserVOList(userList);
        },executors);

        CompletableFuture<Void> query_2 = CompletableFuture.runAsync(() -> {
            List<ViolationTypeStatisticsSeatVO> seatList = violationTypeStatisticsMapper.querySeatList(violationTypeStatisticsQuery);
            violationTypeStatisticsVO.setSeatVOList(seatList);
        },executors);
        CompletableFuture<Void> query_3 = CompletableFuture.runAsync(() -> {
            List<ViolationTypeStatisticsViolationVO> violationList = violationTypeStatisticsMapper.queryViolationList(violationTypeStatisticsQuery);
            violationTypeStatisticsVO.setViolationVOList(violationList);
        },executors);
        CompletableFuture<Void> allFuture = CompletableFuture.allOf(query_1, query_2, query_3);
        try {
            allFuture.get();
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return violationTypeStatisticsVO;
    }

}
